from fastapi import APIRouter, Request

from pyJianYingDraft import CapCut_Video_character_effect_type, Video_character_effect_type, \
    CapCut_Video_scene_effect_type, Video_scene_effect_type
from src.constants.resultcode import ResultCode
from .base import RouterMeta
import config
from src.utils.response import ApiResponse
from src.utils.logger import get_logger
from src.schemas.effect import AddEffectRequest
from src.service.add_effect import add_effect_impl


logger = get_logger("capcut")

router = APIRouter()

@router.get("/character_types",name="获取人物特效")
async def get_character_types(request: Request):
    """
    获取人物特效
    """
    try:
        effect_types = []
        if config.IS_CAPCUT_ENV:
            # Return character effect types in CapCut environment
            for name, member in CapCut_Video_character_effect_type.__members__.items():
                effect_types.append({
                    "character_effect_name": name
                })
        else:
            # Return character effect types in JianYing environment
            for name, member in Video_character_effect_type.__members__.items():
                effect_types.append({
                    "character_effect_name": name
                })
        return ApiResponse.success(
            data=effect_types
        )
    except Exception as e:
        logger.error(f"获取人物特效失败: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"获取人物特效失败: {str(e)}",
        )


@router.get("/scene_types",name="获取场景特效")
async def get_scene_types(request: Request):
    """
    获取场景特效
    """
    try:
        effect_types = []
        if config.IS_CAPCUT_ENV:
            # Return scene effect types in CapCut environment
            for name, member in CapCut_Video_scene_effect_type.__members__.items():
                effect_types.append({
                    "scene_effect_name": name
                })
        else:
            # Return scene effect types in JianYing environment
            for name, member in Video_scene_effect_type.__members__.items():
                effect_types.append({
                    "scene_effect_name": name
                })
        return ApiResponse.success(
            data=effect_types
        )
    except Exception as e:
        logger.error(f"获取场景特效失败: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"获取场景特效失败: {str(e)}",
        )

@router.post("/add_effect", name="添加特效")
async def add_effect(request: Request, effect_request: AddEffectRequest):
    """
    添加特效
    """
    try:
        draft_result = add_effect_impl(
            effect_type=effect_request.effect_type,
            effect_category=effect_request.effect_category,
            start=effect_request.start,
            end=effect_request.end,
            draft_id=effect_request.draft_id,
            track_name=effect_request.track_name,
            params=effect_request.params,
            width=effect_request.width,
            height=effect_request.height
        )

        return ApiResponse.success(data=draft_result)
    except Exception as e:
        logger.error(f"添加特效异常: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"添加特效失败: {str(e)}"
        )



# 路由元数据配置
meta = RouterMeta(
    router=router,
    prefix="/effect",
    tags=["特效"]
)